// Drummer
// by Andrea Bianchi
// andreabianchi.alsoplantsfly.com

/*
 Copyright (C) 2009, Andrea Bianchi
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


#include "raster_graphics.h"
#include "global.h"



s16 DS::clip (s16 v, s16 max)
{
	if (v<0) return 0;
	if (v>max) return max;
	return v;
}

void DS::circlePou16s(u8 screen, s16 x, s16 y, s16 i, s16 j, u16 stroke, u16 fill)
{
	s16 w=WIDTH;
	s16 h=HEIGHT;

	// p=+, m=-
 	s16 xpi=clip(x+i, w);
	s16 xmi=clip(x-i, w);
	s16 xpj=clip(x+j, w);
	s16 xmj=clip(x-j, w);

	s16 ypi=clip(y+i, h);
	s16 ymi=clip(y-i, h);
	s16 ypj=clip(y+j, h);
	s16 ymj=clip(y-j, h);
	
	// fill
	PA_Draw16bitLine (screen, xpi, ypj, xpi, ymj, fill);
	PA_Draw16bitLine (screen, xpj, ymi, xpj, ypi, fill);
	PA_Draw16bitLine (screen, xmi, ymj, xmi, ypj, fill);
	PA_Draw16bitLine (screen, xmj, ypi, xmj, ymi, fill);

	if (stroke==NONE) return;

	// perimeter

	PA_Put16bitPixel(screen ,xpi, ypj, stroke);
	PA_Put16bitPixel(screen ,xpi, ymj, stroke);

	PA_Put16bitPixel(screen ,xpj, ymi, stroke);
	PA_Put16bitPixel(screen ,xpj, ypi, stroke);

	PA_Put16bitPixel(screen ,xmi, ymj, stroke);
	PA_Put16bitPixel(screen ,xmi, ypj, stroke); 

	PA_Put16bitPixel(screen ,xmj, ypi, stroke);
	PA_Put16bitPixel(screen ,xmj, ymi, stroke);
	
}

void DS::circle(u8 screen, s16 x, s16 y, u16 radius, u16 stroke, u16 fill)
{
	s16 i = 0;
	s16 j = radius;
	s16 d = 1 - radius;
	s16 deltaE = 3;
	s16 deltaSE = -2 * radius + 5;
	circlePou16s(screen, x, y, i, j, stroke, fill);

	while (j > i)
	{
		if (d < 0)
		{
			d += deltaE;
			deltaE += 2;
			deltaSE += 2;
		} //if
		else
		{
			d += deltaSE;
			deltaE += 2;
			deltaSE += 4;
			j--;
		} // else
		i++;
		circlePou16s(screen, x, y, i, j, stroke, fill);
	}   
}
